package com.shop.controller;

import com.shop.common.Constants;
import com.shop.model.Address;
import com.shop.model.User;
import com.shop.service.AddressService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * author 何鸿鹰
 * date 2025/5/14 22:49
 * ClassName com.shop.controller
 *
 * @version 1.0
 */
@Slf4j
@Controller
@RequestMapping("/address")
public class AddressController {
    @Autowired
    private AddressService addressService;
    @GetMapping("/list")
    public ModelAndView list(HttpSession session, Model model){
        User user = (User) session.getAttribute(Constants.Session.USER);
        Integer userId = Math.toIntExact(user.getId());
        List<Address> addresses=addressService.selectByUserId(userId);
        model.addAttribute("addresses",addresses);
        return new ModelAndView("user/list");
    }
    @PostMapping("/save")
    public ModelAndView save(@RequestParam("phone")String phone,
                             @RequestParam("address")String address,
                             HttpSession session){
        ModelAndView mv=new ModelAndView();
        if (phone==null||address==null){
            mv.addObject("error", "请填写表格信息后，再提交！");
            mv.setViewName("redirect:/address/list");
        }else {
            if (!phone.matches(Constants.Regex.PHONE)) {
                mv.addObject("error", "电话号码格式错误！");
                mv.setViewName("redirect:/address/list");
            } else {
                User user = (User) session.getAttribute(Constants.Session.USER);
                Integer userId = Math.toIntExact(user.getId());
                Address addressInfo = new Address();
                addressInfo.setAddress(address);
                addressInfo.setPhone(phone);
                addressInfo.setUserId(userId);
                addressService.addAddress(addressInfo);
                mv.setViewName("redirect:/address/list");
            }
        }
        return mv;
    }
    @PostMapping("/update")
    public ModelAndView update(@RequestParam Integer id,
                       @RequestParam String phone,
                       @RequestParam String address,
                       HttpSession session) {
        ModelAndView mv=new ModelAndView();
        if (phone==null||address==null){
            mv.addObject("error", "请填写表格信息后，再提交！");
            mv.setViewName("redirect:/address/list");
        }else {
            if (!phone.matches(Constants.Regex.PHONE)) {
                mv.addObject("error", "电话号码格式错误！");
                mv.setViewName("redirect:/address/list");
            } else {
                User user = (User) session.getAttribute(Constants.Session.USER);
                Integer userId = Math.toIntExact(user.getId());
                Address addrSelect =addressService.getAddressById(id);
                addrSelect.setAddress(address);
                addrSelect.setPhone(phone);
                addressService.updateAddress(addrSelect);
                mv.setViewName("redirect:/address/list");
            }
        }
        return mv;
    }
    @PostMapping("/delete/{id}")
    public ModelAndView delete(@PathVariable Integer id) {
        addressService.deleteAddress(id);
        return new ModelAndView("redirect:/address/list");
    }
}
